package com.zw.pdm.module.craft.service;

import com.zw.pdm.entity.craft.CraMakeDetail;
import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.production.*;
import com.zw.pdm.entity.sys.User;
import com.zw.pdm.module.craft.mapper.CraftReceiveMapper;
import com.zw.pdm.module.craft.mapper.ProductionInputMapper;
import com.zw.pdm.module.craft.model.CarMakeResultModel;
import com.zw.pdm.module.craft.model.CraProQueryModel;
import com.zw.pdm.module.craft.model.ServiceTaskModel;
import com.zw.pdm.module.craft.repository.CraftMakeDetailRepository;
import com.zw.pdm.module.craft.repository.CraftMakeSheetRepository;
import com.zw.pdm.module.report.mapper.ChangeMapper;
import com.zw.pdm.module.report.mapper.PlanMapper;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @Description:
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class ProductionInputService {

    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private ProductionInputMapper productionInputMapper;
    @Autowired
    private ChangeMapper changeMapper;
    @Autowired
    private CraftMakeSheetRepository craftMakeSheetRepository;
    @Autowired
    private CraftMakeDetailRepository craftMakeDetailRepository;

    /**
     * 列表查询
     */
    public List<CraMakeSheet> findWithCraftProduction(CraMakeSheet craMakeSheet) {
        return productionInputMapper.findWithCraftProduction(craMakeSheet);
    }

    /**
     * 通过项目编号查询项目ID
     *
     * @param craMakeSheet
     * @return
     */
    public List<CraMakeSheet> selectProByProCode(CraMakeSheet craMakeSheet) {
        return productionInputMapper.selectProByProCode(craMakeSheet);
    }

    /**
     * 加载所有生产任务
     *
     * @param q
     * @return
     */
    public List<Project> findProjectByCode(String q) {
        return productionInputMapper.findProjectByCode(q);
    }

    /**
     * 保存添加内容
     *
     * @param craMakeSheet
     * @return
     */
    public Integer addCraMakeSheet(CraMakeSheet craMakeSheet) {
        craMakeSheet.setIsRelease(0);
        craMakeSheet.setCreatTime(new Date());
        craMakeSheet.setIsDel(0);
        craMakeSheet.setDrawType(1);
        craMakeSheet.setIsQuota(1);
        craMakeSheet.setIsCommit(1);
        craftMakeSheetRepository.save(craMakeSheet);
        return 1;
    }

    /**
     * 修改新增图号内容
     *
     * @param craMakeSheet
     * @return
     */
    public Integer updateCraMakeSheet(CraMakeSheet craMakeSheet) {
        return productionInputMapper.updateCraMakeSheet(craMakeSheet);
    }

    /**
     * 删除新增图号内容
     *
     * @param craMakeSheet
     * @return
     */
    public Integer updateCraMakeSheet(Integer id) {
        return productionInputMapper.delCraMakeSheet(id);
    }

    /**
     * 通过主表ID查询明细表数据
     *
     * @param sheetId
     * @return
     */
    public List<CraMakeDetail> selectCraMakeDetailBySheetId(Integer sheetId) {
        return productionInputMapper.selectCraMakeDetailBySheetId(sheetId);
    }

    /**
     * 批量导入生产明细表数据
     *
     * @param sheetId
     * @param drawNo
     * @param drawNum
     * @param drawName
     * @param makeType
     * @param remark
     * @param url
     * @param orderNum
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveMakeDetail(Integer sheetId, String[] drawNo, Integer[] drawNum, String[] drawName, String[] makeType, String[] remark, String[] url, Integer[] orderNum) {
        for (int i = 0; i < drawNo.length; i++) {
            CraMakeDetail craMakeDetail = new CraMakeDetail();
            craMakeDetail.setSheetId(sheetId);
            craMakeDetail.setDrawNo(drawNo[i]);
            craMakeDetail.setDrawNum(drawNum[i]);
            craMakeDetail.setDrawName(drawName[i]);

            craMakeDetail.setRemark(remark[i]);
            craMakeDetail.setOrderNum(orderNum[i]);
            craMakeDetail.setIsDel(0);
            craMakeDetail.setCreatName(ShiroUtils.getUser().getName());
            craMakeDetail.setCreatTime(new Date());
            craMakeDetail.setLastTime(new Date());
            craMakeDetail.setChangeStatus(0);
            craMakeDetail.setTechnologyChange(0);
            craMakeDetail.setChangeType(0);

            if (makeType[i].trim().equals("装配件")) {
                craMakeDetail.setMakeType(makeType[i]);
                craMakeDetail.setIsNeedCard(0);
            } else {
                craMakeDetail.setMakeType("加工件");
                if(!makeType[i].trim().equals("加工件")){
                    craMakeDetail.setQuality(makeType[i]);
                }
                craMakeDetail.setIsNeedCard(1);
            }
            craftMakeDetailRepository.save(craMakeDetail);
        }
        return 1;
    }

    public int updateProductDetail(CraMakeDetail craMakeDetail) {
        if(craMakeDetail.getMakeType()!=null&&!craMakeDetail.getMakeType().equals("装配件")){
            craMakeDetail.setQuality(craMakeDetail.getMakeType());
            craMakeDetail.setMakeType("加工件");
            craMakeDetail.setLastTime(new Date());
        }
        return  productionInputMapper.updateProductDetail(craMakeDetail);
    }

    public int saveMakeDetailOne(CraMakeDetail craMakeDetail) {
        craMakeDetail.setIsDel(0);
        craMakeDetail.setCreatName(ShiroUtils.getUser().getName());
        craMakeDetail.setCreatTime(new Date());
        craMakeDetail.setLastTime(new Date());
        craMakeDetail.setChangeStatus(0);
        craMakeDetail.setTechnologyChange(0);
        craMakeDetail.setChangeType(0);

        if (craMakeDetail.getMakeType().trim().equals("装配件")) {
            craMakeDetail.setIsNeedCard(0);
        } else {
            if(!craMakeDetail.getMakeType().trim().equals("加工件")){
                craMakeDetail.setQuality(craMakeDetail.getMakeType());
                craMakeDetail.setMakeType("加工件");
            }
            craMakeDetail.setIsNeedCard(1);
        }
        craftMakeDetailRepository.save(craMakeDetail);
        return 1;
    }
}
